package com.young.mylib.view.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import com.young.mylib.R;

/**
 * Created by Young on 2019/6/24.
 */
public class AlertMsgDialog extends Dialog implements android.view.View.OnClickListener {

    private Context context;
    private String title;
    private String message;
    private String strPositive;
    private String strNegative;
    private OnDialogButtonClickListener listener;
    private boolean cancelable = false;
    private TextView tvTitle;
    private TextView btnPositive;


    /**
     * @param context
     * @param title
     * @param strPositive "确定"类按钮文本
     * @param listener
     */
    public AlertMsgDialog(Context context, String title,
                          String strPositive, OnDialogButtonClickListener listener) {
        super(context, R.style.MyDialog);

        this.context = context;
        this.title = title;

        this.strPositive = strPositive;

        this.listener = listener;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_custom);

        //alertDialog是否可以点击外围消失
        setCanceledOnTouchOutside(cancelable);
        setCancelable(cancelable);

        tvTitle = (TextView) findViewById(R.id.tv_dialog_title);
        tvTitle.setText(this.title);
        btnPositive = (TextView) findViewById(R.id.tv_ok);
        btnPositive.setText(this.strPositive);
        final WindowManager.LayoutParams params = this.getWindow().getAttributes();
        params.gravity = Gravity.CENTER;
        this.getWindow().setAttributes(params);
        btnPositive.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                listener.onDialogButtonClick(true);
                dismiss();
            }
        });
    }

    @Override
    public void onClick(final View v) {
        if (v.getId() == R.id.tv_ok) {
            listener.onDialogButtonClick(true);
        }
//        else if (v.getId() == R.id.tv_dialog_negative) {
//            listener.onDialogButtonClick(false);
//        }
        dismiss();
    }


    /**
     * 自定义Dialog监听器
     */
    public interface OnDialogButtonClickListener {

        /**
         * 点击按钮事件的回调方法
         *
         * @param isPositive
         */
        void onDialogButtonClick(boolean isPositive);
    }
}
